package com.banyuan.servlet.author;

import com.alibaba.fastjson.JSON;
import com.banyuan.dao.AuthorDao;
import com.banyuan.domain.Author;
import com.banyuan.domain.ResData;
import com.banyuan.utils.CommonUtils;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.DataBufferUShort;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/author/updateInfo")
public class UpdateInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String nickName = req.getParameter("nickName");

        Integer intId = Integer.valueOf(id);

        // 1.判断传入的参数是否为空
        if (CommonUtils.isEmpty(id) ||  CommonUtils.isEmpty(nickName) ) {
            ResData resData = new ResData(10001,"输入项为空",null);
            resp.getWriter().write(JSON.toJSONString(resData));
            return;// 阻断代码的运行
        }
        // 2.判断传入的id和session中的id是否为同一个，不然就可以通过传参数瞎改了
        HttpSession session = req.getSession();
        Author author = (Author) session.getAttribute("author");
        if (!intId.equals(author.getId())) {
            ResData resData = new ResData(10002,"不可以修改其他用户的昵称",null);
            resp.getWriter().write(JSON.toJSONString(resData));
            return;// 阻断代码的运行
        }

        // 3.修改数据库的昵称，
        // 在修改前，一定要先查后改,最后把session里面的author也更新一下，不然前端还是展示以前的
        ResultSet resultSet = null;
        ResultSet rs = null;
        try {
            // 先通过id查找Author的信息，通过反射生成Author对象
             resultSet = AuthorDao.selectAuthorById(intId);
            Author oldAuthor = DBUtils.resultToBeanOne(resultSet, Author.class);
            oldAuthor.setNickName(nickName);

            // 修改

            int i = AuthorDao.updateAuthorById(oldAuthor);
            if (i > 0) {
                ResData resData = new ResData(200,"修改成功",null);
                resp.getWriter().write(JSON.toJSONString(resData));
                //查出修改完的修改时间和nickName
                rs = AuthorDao.selectAuthorById(intId);
                Author newAuthor = DBUtils.resultToBeanOne(rs, Author.class);
                session.setAttribute("author",newAuthor);
                // 更新session里面的author
            } else {
                ResData resData = new ResData(10003,"修改失败，稍后再试",null);
                resp.getWriter().write(JSON.toJSONString(resData));
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
            JDBCUtils.closeBy(rs);

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
